package mww.servlet.student;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import mww.common.DaoException;
import mww.common.ServiceException;
import mww.javabean.service.StudentCService;
import mww.javabean.service.StudentCServiceImpl;
import mww.javabean.service.StudentService;
import mww.javabean.service.StudentServiceImpl;
public class DelStudentServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		StudentService studentService = new StudentServiceImpl();
		StudentCService scService = new StudentCServiceImpl();
		HttpSession session=request.getSession();
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		
		String studentNo=request.getParameter("studentNo");
		try{
			//删除学生记录前判断从表中是否存在关联记录(选课信息)，若不存在关联进行删除，否则禁止删除
			List list=scService.findStudentCByStudentNo(studentNo);
			if(list!=null&&list.size()>0){
				session.setAttribute("message", "所要删除记录存在关联项(选课信息)，禁止删除该学生信息！");
		  		request.getRequestDispatcher("/admin/listAllStudentServlet").forward(request, response);
			}else{
		  		studentService.delStudent(studentNo);
		  		session.setAttribute("message", "删除学生信息成功！");
		  		request.getRequestDispatcher("/admin/listAllStudentServlet").forward(request, response);
			}
		}catch (ServiceException e) {
			e.printStackTrace();
			request.setAttribute("se", e);
			request.getRequestDispatcher("/serviceException.jsp").forward(request, response);
		} catch (DaoException e) {
			e.printStackTrace();
			request.setAttribute("de", e);
			request.getRequestDispatcher("/daoException.jsp").forward(request, response);
		}
	}
}
